/*
mode by xiaoxiaoyudu
github :https://github.com/xiaoxiaoyudu
qq :1907383069
blbl:两位真实好友
部分参考正点原子
*/
#include "gbk.h"
extern FontxFile fx16M[2];
extern TFT_t DEV;
void Get_HzMat(char *code,unsigned char *mat,unsigned char size)
{		    
	unsigned char qh,ql;					  
	unsigned long foffset; 
	char file[30];
	unsigned char csize=(size/8+((size%8)?1:0))*(size);//得到字体一�?字�?��?�应点阵集所占的字节�?	 
		qh=*code;
		ql=*(++code);
		ql -= 0xa1;
		qh -= 0xa1;
        foffset = ((unsigned long)94*qh + ql)*csize;
	
	//w25qxx_read_data(GB32+foffset, mat, csize, W25QXX_QUAD_FAST);

	//读取文件
	strcpy(file, "/spiffs/2312.DZK");
	FILE* fp = fopen(file, "rb");
	if (fp == NULL) {
	ESP_LOGW(__FUNCTION__, "File not found [%s]", file);
	return ;
	}
	fseek(fp, foffset, SEEK_SET );
	fread(mat, 1,csize, fp);	 
	fclose(fp);
											    
}  
void LCD_ShowChinese_buff(TFT_t * dev,uint16_t x, uint16_t y,  uint16_t color,unsigned char * bin)	
{  
	unsigned char i,j;
	unsigned char *temp;
	temp=(unsigned char *)bin;

	for(j=0;j<32;j++)
	{
		for(i=0;i<8;i++)
		{
		 	if((*temp&(1<<i))!=0)//´ÓÊý¾ÝµÄµÍÎ»¿ªÊ¼¶Á
			{
				lcdDrawPixel(dev, j%16+x,y+((j>15)?i+8:i), color);
			}
			else
			{
				
			}
		}
		temp++;
	 }
}
//在指定位置开始显示一个字符串	    
//支持自动换行
//(x,y):起始坐标
//width,height:区域
//str  :字符串
//size :字体大小
//mode:0,慢速;1,快速 
void Lcd_draw_chinese(TFT_t * dev,uint16_t x, uint16_t y,  uint16_t color,char * str)
{
        u8 bin[32];
        Get_HzMat(str,bin,16);
		//LCD_ShowChinese_buff(&DEV,x, y, color,bin);
        LCD_fast_ShowChinese_buff(&DEV,x, y, color,bin);
}  	   		   

void Show_Str(u16 x,u16 y,u16 width,u16 height,char *str,u8 size, uint16_t color)
{					
	u16 x0=x;
	u16 y0=y;							  	  
    u8 bHz=0;     //字符或者中文 1中文 0英语 	    				    				  	  
    while(*str!=0)//数据未结束
    { 
        if(!bHz)//英语
        {
	        if(*str>0x80)bHz=1;//中文 
	        else              //字符
	        {      
                if(x>(x0+width-size/2))//换行
				{				   
					y+=size;
					x=x0;	   
				}							    
		        if(y>(y0+height-size))break;//越界返回      
		        if(*str==13)//换行符号
		        {         
		            y+=size;
					x=x0;
		            str++; 
		        }  
		        else 
		   
		        	
		     // lcd_draw_char(x, y, *str, color) ;
		    lcdDrawChar(&DEV,fx16M,  x,  y+16, *str, color);
		        
				str++; 
		        x+=size/2; //字符,为全字的一半 
	        }
        }else//中文 
        {     
            bHz=0;//有汉字库    
            if(x>(x0+width-size))//换行
			{	    
				y+=size;
				x=x0;		  
			}
	        if(y>(y0+height-size))break;//越界返回  				     
	        //Show_Font(x,y,str,size,mode); //显示这个汉字,空心显示

            Lcd_draw_chinese(&DEV,x,y,color,str);
	        str+=2; 
	        x+=size;//下一个汉字偏移	    
        }						 
    }   
}
//指定颜色
void Show_Str_Mid(u16 x,u16 y,char*str,u8 size,u8 len,uint16_t color)
{
	u16 strlenth=0;
   	strlenth=strlen((const char*)str);
	strlenth*=size/2;
	if(strlenth>len)Show_Str(x,y,240,135,str,size,color);
	else
	{
		strlenth=(len-strlenth)/2;
	    Show_Str(strlenth+x,y,240,135,str,size,color);
	}
} 
void LCD_fast_ShowChinese_buff(TFT_t * dev,uint16_t x, uint16_t y,  uint16_t color,unsigned char * bin)	
{  
	unsigned char i,j;
	unsigned char *temp;
	temp=(unsigned char *)bin;
	uint16_t* data = (uint16_t*) malloc(16*16*2);
	memset(data,0,16*16*2);

	for(j=0;j<32;j++)
	{
		for(i=0;i<8;i++)

		{
		 	if((*temp&(1<<i))!=0)//´ÓÊý¾ÝµÄµÍÎ»¿ªÊ¼¶Á
			{
				*(data+((j>15)?i+8:i)*16+j%16)=color;
				//lcdDrawPixel(dev, j%16+x,y+((j>15)?i+8:i), color);
			}
			else
			{
				
			}
		}
		temp++;
	 }
	lcd_fastDraw_xy(&DEV,x,y,16,16,(uint8_t *)data);
	 free(data);


}
void LCD_draw_ram_chinese(TFT_t * dev,uint16_t x, uint16_t y,  uint16_t color,char * str,uint16_t *data,int w)
{  unsigned char i,j,bin[32];
	unsigned char *temp;
	Get_HzMat(str,bin,16);

	temp=(unsigned char *)bin;

	for(j=0;j<32;j++)
	{
		for(i=0;i<8;i++)

		{
		 	if((*temp&(1<<i))!=0)//´ÓÊý¾ÝµÄµÍÎ»¿ªÊ¼¶Á
			{

				*(data+(((j>15)?i+8:i)+y)*w+j%16+x)=color;
				//*(data+((j>15)?i+8:i)*16+j%16)
			}
			else
			{
				
			}
		}
		temp++;
	 }
	// lcd_fastDraw_xy(&DEV,x,y,16,16,(uint8_t *)data);
	//  free(data);
	
}
void Show_Mid_Ram(u16 x,u16 y,char*str,u8 size,u8 len,uint16_t color,uint16_t *data,int w,int h)
{
	u16 strlenth=0;
   	strlenth=strlen((const char*)str);
	strlenth*=size/2;
	if(strlenth>len)Show_Str_Ram(x,y,w,h,str,size,color,data);
	else
	{
		strlenth=(len-strlenth)/2;
	    Show_Str_Ram(strlenth+x,y,w,h,str,size,color,data);
	}
} 
void Show_Str_Ram(u16 x,u16 y,u16 width,u16 height,char *str,u8 size, uint16_t color,uint16_t *data)
{					
	u16 x0=x;
	u16 y0=y;							  	  
    u8 bHz=0;     //字符或者中文 1中文 0英语 	    				    				  	  
    while(*str!=0)//数据未结束
    { 
        if(!bHz)//英语
        {
	        if(*str>0x80)bHz=1;//中文 
	        else              //字符
	        {      
                if(x>(x0+width-size/2))//换行
				{				   
					y+=size;
					x=x0;	   
				}							    
		        if(y>(y0+height-size))break;//越界返回      
		        if(*str==13)//换行符号
		        {         
		            y+=size;
					x=x0;
		            str++; 
		        }  
		        else 
		   
		        	
		     // lcd_draw_char(x, y, *str, color) ;
		   // lcdDrawChar(&DEV,fx16M,  x,  y+16, *str, color);
		   // lcd_fastDrawChar_ram(&DEV,fx16M,  x,  y, *str, color,data);
			//lcd_RAMDrawString(fx16M, x, y,*str, WHITE,data,180);
			lcd_fastDrawChar_ram(fx16M, x, y, *str, WHITE,data,width);
				str++; 
		        x+=size/2; //字符,为全字的一半 
	        }
        }else//中文 
        {     
            bHz=0;//有汉字库    
            if(x>(x0+width-size))//换行
			{	    
				y+=size;
				x=x0;		  
			}
	        if(y>(y0+height-size))break;//越界返回  				     
	        //Show_Font(x,y,str,size,mode); //显示这个汉字,空心显示

           // Lcd_draw_chinese(&DEV,x,y,color,str);
			LCD_draw_ram_chinese(&DEV,x,y,color,str,data,width);
	        str+=2; 
	        x+=size;//下一个汉字偏移	    
        }						 
    }   
}